package domain

import (
	"github.com/lab-online/internal/user/constant"
	"github.com/lab-online/internal/user/entity"
)

func (d *Domain) Login(
	user entity.UserEntity,
) (accessToken string, refreshToken string, e error) {
	registeredUser, err := d.repository.GetUserByUsername(user.GetUsername())
	if err != nil {
		return "", "", constant.RESTErrUserNotMatch
	} else if !registeredUser.ComparePassword(user.GetPassword()) {
		return "", "", constant.RESTErrUserNotMatch
	}

	accessToken, err = d.jwt.GenToken(registeredUser.GetUsername(), registeredUser.GetRole())
	refreshToken, err2 := d.jwt.GenToken(registeredUser.GetUsername(), uint8(0))
	if err != nil || err2 != nil {
		return "", "", constant.RESTErrTokenGenError
	}
	return accessToken, refreshToken, nil
}
